﻿myApp.controller('AuctionDetailController', function ($scope, $http, $routeParams,$filter,NgMap,$rootScope, $location, $rootScope) {
  
    $scope.isSearch = true;
    $scope.isAndroid = $rootScope.isAndroid;


    pageInit();

    $scope.isLogin = $rootScope.isAuth;


    var BASE_URL;
    if ($location.host() == "localhost") {
        BASE_URL = $location.protocol() + "://" + "localhost:" + $location.port();
    } else {
        BASE_URL = $location.protocol() + "://" + $location.host();
    }

    $scope.current_path_url = $location.absUrl();


    function pageInit(){ 
        if ($routeParams.id) {
            var data = sessionStorage.getItem($routeParams.id);
            console.log("DATA!!!");
            console.debug("%o", data);

            // if no data in session, get from the server
            jsonObj = JSON.parse(data);
            $scope.property = jsonObj;

            var url = 'api/Auction/GetAuction/' + $routeParams.id;
            $http.get(url).success(function (data) {
                console.log("AUCTION DATA: " + JSON.stringify(data));

                $scope.Auction = data.Auction;
                $scope.Auction.StartDateTime = new Date($scope.Auction.StartDateTime);
                $scope.Auction.EndDateTime = new Date($scope.Auction.EndDateTime);
                $scope.slides = data.pictureList;
                if (!$scope.slides) {
                    $scope.slides = [];
                    $scope.slides.push({ pictureURL: 'http://www.penangrealty.my/images/penang realty,Apartment,house,condominium,terrace house,semi d,detached,bungalow,high end luxury properties,landed properties,gated guarded house.png' });
                }

                $scope.shareTitle = data.Auction.Description;
                $scope.shareDescription = data.Auction.RoadName;
                $scope.shareURL = BASE_URL + "/property/auctions/details/" + data.Auction.LelongId;
                $scope.shareImageURL = data.Auction.PropertyURL || "http://www.penangrealty.my/images/penang realty,Apartment,house,condominium,terrace house,semi d,detached,bungalow,high end luxury properties,landed properties,gated guarded house.png";

                $scope.googleShareURL = encodeURI(encodeURI($scope.shareURL));

                $scope.isSearch = false;
            }).error(function (data) {
                console.log("ERROR: " + data);
                $scope.isSearch = false;

            });
        }
    }

    $scope.submitForm = function (obj) {
        var url = 'https://script.google.com/macros/s/AKfycbwcomu5o2CIbkO1lCjyZhDrF_dejSPWi0VAj9knYKYRr3jHDUY/exec';
        $http({
            method: 'POST',
            url: url,
            data: $(obj.target).serialize(),
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function (data) {

            alert("Thanks for Subscribing to our Newsletter!");
            $scope.subscription_email = "";
        });
    }




});